﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NATP2P.STUN;
using NATP2P.UDPNat;
using System.Net;
namespace ChatCam.Gui
{
    /// <summary>
    /// Interaction logic for Config.xaml
    /// </summary>
    public partial class Config : Window
    {
        public Config()
        {
            InitializeComponent();
        }

        private UDPNatSocket _conhandler;

        public UDPNatSocket ConHandler
        {
            get { return _conhandler; }
            set { _conhandler = value; }
        }

        EndPointMapRequest res;
        private void btnmap_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                 res = new EndPointMapRequest(2013, "stun.stunprotocol.org");
                 res.Send();
                 txtEip.Text = res.ExternalEndPoint.Address.ToString();
                 txtEport.Text = res.ExternalEndPoint.Port.ToString();
            }
            catch (Exception msg)
            {

                MessageBox.Show(msg.StackTrace,msg.Message);
            }

        }

        UDPNatSocket conHandler;
        private void btnconnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                conHandler = new UDPNatSocket(res, new IPEndPoint(IPAddress.Parse(txtRip.Text), int.Parse(txtRport.Text)), (TypeNat)Enum.Parse(typeof(TypeNat), comboBox1.SelectionBoxItem.ToString(), true));
                conHandler.Connect();
                this.ConHandler = conHandler;

            }
            catch (Exception msg)
            {

                MessageBox.Show(msg.StackTrace, msg.Message);
            }

        }

        private void btnlisten_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                conHandler = new UDPNatSocket(res, new IPEndPoint(IPAddress.Parse(txtRip.Text), int.Parse(txtRport.Text)), (TypeNat)Enum.Parse(typeof(TypeNat), comboBox1.SelectionBoxItem.ToString(), true));
                conHandler.Listen();
                this.ConHandler = conHandler;
            }
            catch (Exception msg)
            {

                MessageBox.Show(msg.StackTrace, msg.Message);
            }
        }
        
        

        
    }
}
